package com.xianyl.libsforandroid.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;


/**
 * 屏幕工具类
 * @author xianyl
 * @date 2015-3-2 下午9:37:57
 */
public class ScreenUtils {
	private static int mWidth = 0;
	private static int mHeight = 0;
	private static float mDensity = 0;
	private static float mDensityDpi = 0;
	private static float mScaledDensity = 0;
	
	
    
	/**
	 * 获取屏幕管理器 ,如果context为空，则返回空 
	 * @param context
	 * @return
	 */
	public static WindowManager getWindowManager(Context context) {
		if(context == null)
			return null;		
		return (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		
	}
	
	/**
	 * 获取屏幕宽度，像素值，如果context为空，则返回0
	 * @param context
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static int getScreenWidth(Context context) {
		if(mWidth != 0)
			return mWidth;
		WindowManager manager = getWindowManager(context);
		if(manager != null)
			mWidth = manager.getDefaultDisplay().getWidth();
		return mWidth;
		
	}
	
	/**
	 * 获取屏幕高度，像素值，如果context为空，则返回-1
	 * @param context
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static int getScreenHeight(Context context) {
		if(mHeight != 0)
			return mHeight;
		WindowManager manager = getWindowManager(context);
		if(manager != null)
			mHeight = manager.getDefaultDisplay().getHeight();
		return mHeight;
		
	}
	
	/**
	 * 获取DisplayMetrics对象 ，该对象可以获取手机屏幕信息，如果context为空，则创建一个DisplayMetrics对象
	 * @param context
	 * @return
	 */
	public static DisplayMetrics getScreenDisplayMetrics (Context context) {
		DisplayMetrics dm = null;
		if(context != null)
			dm = context.getResources().getDisplayMetrics();
		WindowManager manager = getWindowManager(context);
		if(dm == null)
			dm = new DisplayMetrics();
		if(manager != null)
			manager.getDefaultDisplay().getMetrics(dm);
		return dm;
		
	}
	
	/**
	 * 获取屏幕密度（像素比例：0.75/1.0/1.5/2.0）  
	 * @param context
	 * @return
	 */
	public static float getScreenDensity (Context context) {
		if(mDensity == 0)
			mDensity = getScreenDisplayMetrics(context).density;
		return mDensity;
		
	}
	/**
	 * 屏幕密度（每寸像素：120/160/240/320  
	 * @param context
	 * @return
	 */
	public static float getScreenDensityDpi (Context context) {
		if(mDensityDpi == 0)
			mDensityDpi = getScreenDisplayMetrics(context).densityDpi;
		return mDensityDpi;
		
	}
	/**
	 * 字体密度
	 * @param context
	 * @return
	 */
	public static float getScreenScaledDensity (Context context) {
		if(mScaledDensity == 0)
			mScaledDensity = getScreenDisplayMetrics(context).scaledDensity;
		return mScaledDensity;
		
	}
	
	
}
